package com.xueyi.system.inspection.controller;

import com.xueyi.system.api.dict.domain.dto.SysDictDataDto;
import com.xueyi.system.api.dict.domain.query.SysDictDataQuery;
import com.xueyi.system.dict.service.ISysDictDataService;
import com.xueyi.system.inspection.domain.query.InspectionQuery;
import com.xueyi.system.inspection.domain.dto.InspectionDto;
import com.xueyi.system.inspection.service.IInspectionService;
import com.xueyi.common.web.entity.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.Logical;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * 田间巡检管理 业务处理
 *
 * @author zlf
 */
@RestController
@RequestMapping("/inspection")
public class InspectionController extends BaseController<InspectionQuery, InspectionDto, IInspectionService> {


    @Autowired
    private IInspectionService iInspectionService;

    @Autowired
    private ISysDictDataService dictDataService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "田间巡检" ;
    }

    /**
     * 查询田间巡检列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.INSPECTION_LIST)
    public AjaxResult list(InspectionQuery inspection) {
        return super.list(inspection);
    }

    /**
     * 查询田间巡检详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.INSPECTION_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 田间巡检导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.INSPECTION_EXPORT)
    @Log(title = "田间巡检管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, InspectionQuery inspection) {
        super.export(response, inspection);
    }

    /**
     * 田间巡检新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.INSPECTION_ADD)
    @Log(title = "田间巡检管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody InspectionDto inspection) {
        return super.add(inspection);
    }

    /**
     * 田间巡检修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.INSPECTION_EDIT)
    @Log(title = "田间巡检管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody InspectionDto inspection) {
        return super.edit(inspection);
    }

    /**
     * 田间巡检修改状态
     */
    @Override
    @PutMapping("/status")
    @RequiresPermissions(value = {Auth.INSPECTION_EDIT, Auth.INSPECTION_ES}, logical = Logical.OR)
    @Log(title = "田间巡检管理", businessType = BusinessType.UPDATE_STATUS)
    public AjaxResult editStatus(@RequestBody InspectionDto inspection) {
        return super.editStatus(inspection);
    }

    /**
     * 田间巡检批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.INSPECTION_DEL)
    @Log(title = "田间巡检管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 获取田间巡检选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }


    @GetMapping("/getInspection")
    public AjaxResult getInspection(String inspectionDtoId) {
        InspectionDto inspectionDto = iInspectionService.selectById(inspectionDtoId);
        SysDictDataQuery dictDataQuery = new SysDictDataQuery();
        dictDataQuery.setCode("status_type");
        dictDataQuery.setValue(inspectionDto.getStatus());
        List<SysDictDataDto> dataDtoList = dictDataService.selectList(dictDataQuery);
        inspectionDto.setStatus(dataDtoList.get(0).getLabel());

        SysDictDataQuery dataQuery = new SysDictDataQuery();
        dataQuery.setCode("base_inspection_type");
        dataQuery.setValue(inspectionDto.getType());
        List<SysDictDataDto> dtoList = dictDataService.selectList(dictDataQuery);
        inspectionDto.setType(dtoList.get(0).getLabel());

        return AjaxResult.success(inspectionDto);
    }

    interface Auth {
        /** 系统 - 田间巡检管理 - 列表 */
        String INSPECTION_LIST = "system:inspection:list" ;
        /** 系统 - 田间巡检管理 - 详情 */
        String INSPECTION_SINGLE = "system:inspection:single" ;
        /** 系统 - 田间巡检管理 - 新增 */
        String INSPECTION_ADD = "system:inspection:add" ;
        /** 系统 - 田间巡检管理 - 修改 */
        String INSPECTION_EDIT = "system:inspection:edit" ;
        /** 系统 - 田间巡检管理 - 修改状态 */
        String INSPECTION_ES = "system:inspection:es" ;
        /** 系统 - 田间巡检管理 - 删除 */
        String INSPECTION_DEL = "system:inspection:delete" ;
        /** 系统 - 田间巡检管理 - 导出 */
        String INSPECTION_EXPORT = "system:inspection:export" ;
    }
}
